package com.ruoyi.develop.lnGongxu.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.UserConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lnGongxu.domain.LnGongxu;
import com.ruoyi.develop.lnGongxu.service.ILnGongxuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工序Controller
 * @author qh
 * @date 2024-07-21
 */
@RestController
@RequestMapping("/lnGongxu/lnGongxu")
public class LnGongxuController extends BaseController {
    @Autowired
    private ILnGongxuService lnGongxuService;

    /**
     * 查询工序列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongxu:query')")
    @GetMapping("/list")
    public TableDataInfo list(LnGongxu entity) {
        startPage();
        List<LnGongxu> list = lnGongxuService.selectList(entity);
        return getDataTable(list);
    }

    @GetMapping(value = "/selectMaxCode")
    public AjaxResult selectMaxCode() {
        return AjaxResult.success(lnGongxuService.selectMaxCode());
    }

    /**
     * 导出工序列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongxu:export')")
    @Log(title = "工序", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LnGongxu entity) {
        List<LnGongxu> list = lnGongxuService.selectList(entity);
        ExcelUtil<LnGongxu> util = new ExcelUtil<LnGongxu>(LnGongxu. class);
        util.exportExcel(response, list, "工序数据");
    }

    /**
     * 获取工序详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongxu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lnGongxuService.selectById(id));
    }

    /**
     * 新增工序
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongxu:add')")
    @Log(title = "工序", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LnGongxu entity) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = sdf.format(new Date());
        entity.setCode(time);
        if (UserConstants.NOT_UNIQUE.equals(lnGongxuService.checkCodeUnique(entity.getCode()))) {
            return AjaxResult.error("新增工序编号'" + entity.getCode() + "'失败，工序编号已存在");
        }
        return toAjax(lnGongxuService.insert(entity));
    }

    /**
     * 修改工序
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongxu:edit')")
    @Log(title = "工序", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LnGongxu entity) {
        return toAjax(lnGongxuService.update(entity));
    }

    /**
     * 批量修改图纸号
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongxu:edit')")
    @Log(title = "批量修改图纸号", businessType = BusinessType.UPDATE)
    @PutMapping("/editAll")
    public AjaxResult editAll(@RequestBody LnGongxu entity) {
        return toAjax(lnGongxuService.updateAll(entity));
    }
    /**
     * 删除工序
     */
    @PreAuthorize("@ss.hasPermi('develop:LnGongxu:remove')")
    @Log(title = "工序", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lnGongxuService.deleteByIds(ids));
    }
}
